<template>
<div class="waper_page">
  <div style="padding-bottom:10px">
    <div>vue2.0的移动端的touch事件</div>
    <div style="padding-left:10px">
      <div>① 开始事件是@touchstart</div>
      <div>② 移动过程是@touchmove</div>
      <div>③ 结束事件是@touchend</div>
    </div>
  </div>
  <div class="touch" v-for="(item, index) in listData" :key="index">
    <!-- 会话列表 -->
    <div class="list_box" :data-index="index" :style="item.moveStyle" @click="gotoChat(item)"
      @touchstart.stop.prevent="touchstart" @touchmove.stop.prevent="touchmove" @touchend.stop.prevent="touchend">
      <!-- @touchstart.capture="touchstart" @touchmove.capture="touchmove" @touchend.capture="touchend" -->
      <!-- 头像 -->
      <div class="list_img">
        <img :src="item.imgUrl">
      </div>
      <!-- 提示 -->
      <div class="list_msg" v-if="item.count">
        <div class="list_msg_icon">{{item.count}}</div>
      </div>
      <!-- 消息 -->
      <div class="list_info">
        <div class="name">
          <div style="font-size:16px;font-weight:700;color:333;">{{item.name}}</div>
          <div style="font-size:14px;padding-right:15px;">{{item.time}}</div>
        </div>
        <div class="text">
          <div style="font-size: 12px;">{{item.text}}</div>
        </div>
      </div>
    </div>
    <!-- 删除 -->
    <div class="delete" @click="deleteItem(item.conversationID)">删除</div>
  </div>
</div>
</template>

<script>
export default {
  data () {
    return {
      listData:[
        {
          imgUrl: require('@/assets/img/face_4.jpg'),
          count:5,
          text:'你好',
          name:'lily',
          time:'15:30'
        },
        {
          imgUrl:require('../../assets/img/face_5.jpg'),
          count:4,
          text:'你好',
          name:'lily',
          time:'15:30'
        },
        {
          imgUrl:require('../../assets/img/face_6.jpg'),
          count:3,
          text:'你好',
          name:'lily',
          time:'15:30'
        },
        {
          imgUrl:require('../../assets/img/face_9.jpg'),
          count:2,
          text:'你好',
          name:'lily',
          time:'15:30'
        },
        {
          imgUrl:require('../../assets/img/face_13.jpg'),
          count:1,
          text:'你好',
          name:'lily',
          time:'15:30'
        }
      ],
      startX: 0, //开始坐标
      index: '', //滑动的元素
    }
  },
  methods: {
    // 跳转
    gotoChat () {
    //   this.$router.push({
    //     path: '/detail'
    //   })
    },
    //手指触摸动作开始 记录起点X坐标
    touchstart(e) {
      console.log('开始移动', e)
      // 获得起始坐标
      this.startX = e.changedTouches[0].pageX;  //pageX：距离文档左上角的距离，文档的左上角为原点 ，横向为X轴，纵向为Y轴
      // 获得滑动元素的索引
      this.index = e.currentTarget.dataset.index;
    },
    //滑动事件处理
    touchmove(e) {
      console.log('移动中', e)
      let _this = this;
      // 获得当前坐标
      let currentX = e.changedTouches[0].pageX;
      // 当前数据
      let list = _this.listData;
      // 计算滑动距离
      const moveLeft = this.startX - currentX;
      list[_this.index].left = moveLeft > 0 ? -moveLeft : 0;
      // 移动样式的变化
      let moveStyle = "";
      if(moveLeft > 0){
        // moveStyle = "transform: translateX(" + list[_this.index].left + "px);"+ "transition: all 0.4s;";
        moveStyle = "transform: translateX(-150px);"+ "transition: all 0.4s;";
      }
      list[_this.index].moveStyle = moveStyle; 
      _this.listData = list;
      
    },
    // 移动结束(手拿开)
    touchend(e){
      console.log('移动结束', e)
      let _this = this;
      let list = _this.listData;
      // 计算最后移动的距离
      let lastMove = _this.startX - e.changedTouches[0].pageX;
      list[_this.index].left = lastMove > 100 ? -180 : 0;
      _this.listData = list;
    },
    // 删除
    deleteItem (e) {
      // 当前索引
      let index = e.currentTarget.dataset.index;
      // 删除
      this.list.splice(index, 1);
      // splice() 方法向/从数组中添加/删除项目，然后返回被删除的项目。
      // slice() 方法可从已有的数组中返回选定的元素。
      // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
    }
  }
}
</script>

<style lang="less" scoped >
/* 左滑删除 */
.touch{
  width: 100%;
  height: 65px;
  margin: 5px 0;
  border-bottom: 1px solid #eee;
  position:relative;
  box-sizing: border-box;
}
.touch .delete{
  position: absolute;
  right: 0px;
  top: 0;
  z-index: 1;
  width: 150px;
  height: 100%;
  line-height: 150px;
  background-color: #e64340;
  font-size: 28px;
  color: #fff;
  text-align: center;
}
/* 会话列表 */
.touch .list_box{
  width: 100%;
  height: 100%;
  background-color: #FFFFFF;
  display: flex;
  flex-direction: row;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 99999;
  box-sizing: border-box;
  overflow: hidden;
}
.list_box .list_img{
  display: flex;
  align-items: center;
  width: 55px;
  height: 50px;
  margin: 5px;
  border-radius: 10px;
  border: 1px solid #CDCDCD;
}
.list_box .list_img img{
  width: 100%;
  height: 100%;
}
.list_box .list_msg{
  width: 15px;
  height: 15px;
  line-height: 15px;
  text-align: center;
  background: #f06c7a;
  color: #fff;
  font-size: 8px;
  border-radius: 50%;
  position: relative;
  top: 0px;
  right: 12px;
}
.list_box .list_msg_icon{
  position: absolute;
  left: 4.5px;
}
.list_box .list_info{
  width: 80%;
  display: flex;
  flex-direction: column;
}
.list_box .name{
  padding-top: 5px;
  display: flex;
  justify-content: space-between;
}
.list_box .text{
  padding-top: 5px;
  height: 25px;
  line-height: 25px;
  color: #666;
  text-overflow: ellipsis;
  white-space: nowrap;
}

</style>